<template>
    <div class="journalism">
        <div class="Top">新闻资讯</div>
        <!-- 新闻列表 -->
        <div class="newslist">
            <div class="newsli" v-for="(item, index) in newsList" :key="index" @click="ToContent(item.id)">
                <div class="newsTop">
                    <div class="badge" :style="{ backgroundColor: item.sentiment === '利好' ? 'red' : '#008000c7' }">
                        {{ item.sentiment }}</div>
                    <div>
                        <h3 class="news_title" :style="{ 'color': getHasCheckedJournalism(item.id) ? '#969696' : 'black' }">
                            {{ item.title }}</h3>
                        <div class="relatedStockArea">
                            <div class="relatedStock">#{{ item.stockCode + ' ' + item.stockName }}</div>
                            <div class="relatedStock newsType">{{ item.newsType }}</div>
                        </div>
                    </div>
                    <div class="news_time">{{ item.originName + ' ' + item.publishDate }}</div>
                </div>
                <div class="news_content_area">
                    <div class="news_content">{{ item.content }}</div>
                    <div v-if="item.imageSrc !== null">
                        <img class="news_img" :src="item.imageSrc" alt="">
                    </div>
                    <div v-if="item.imageSrc === null" style="width: 245px;height: 156px;"></div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { onMounted, ref, watch } from 'vue'
import { updateJournalismList } from "@/api/request";
import {useRouter} from 'vue-router'


const props = defineProps({
    stockId: String
})

const Router = useRouter()


const { stockId } = props

const newsList = ref([])

let rangSearchObject = {
    stockId: stockId,
}

// 判断是否已经点开过这条新闻，点开过的新闻标题会变灰
function getHasCheckedJournalism(id) {
    let data = JSON.parse(localStorage.getItem('hasCheckedJournalism')) || [];
    return data.indexOf(id.toString()) !== -1;
}

const getNewsList = () => {
    updateJournalismList(rangSearchObject).then(res => {
        newsList.value = res.records
        for (let i = 0; i < newsList.value.length; i++) {
            switch (newsList.value[i].sentiment) {
                case "neutral":
                    newsList.value[i].sentiment = '中性';
                    break;
                case "positive":
                    newsList.value[i].sentiment = '利好';
                    break;
                case "negative":
                    newsList.value[i].sentiment = '利空';
                    break;
            }
        }
    })
}

function ToContent(id) {
    Router.push({
        path: '/journalismContent',
        query: { jourId: id }
    })
}

onMounted(() => {
    getNewsList()
})



</script>

<style scoped>
.journalism {
    background: white;
    border-radius: 52px;
    margin-top: 18px;
    padding: 19px 19px 40px;
}

.Top {
    border-left: 7px solid #3377ff;
    padding-left: 16px;
    font-size: 16px;
    font-weight: 800;
    margin-bottom: 8px;
}

.classfiyNews {
    display: flex;
    border-top: 1px solid #b3acac;
    padding-top: 14px;
    margin-top: 12px;
}

.news_type {
    margin-left: 36px;
    line-height: 30px;
}

.tips {
    line-height: 30px;
    margin-left: 22px;
    margin-right: 15px;
}

.jourList {
    display: flex;
    font-size: 19px;
    border-bottom: 1px solid #b3acac;
    margin-top: 12px;
    padding-bottom: 8px;
}

.jourList div:nth-child(1) {
    width: 33%;
    margin-left: 12px;
}

.jourList div:nth-child(2) {
    width: 14%;
}

.jourList div:nth-child(3) {
    width: 51%;
}

.importentList {
    display: flex;
    margin-top: 16px;
    border-bottom: 1px solid #b3acac;
    padding-bottom: 16px;
    font-size: 14px;
    cursor: pointer;
}

.importentList div:nth-child(1) {
    width: 33%;
    margin-left: 12px;
}

.importentList div:nth-child(2) {
    width: 14%;
}

.importentList div:nth-child(3) {
    width: 51%;
}

.newslist {
    /*background-color: #1890ff;*/
    padding: 10px;
    margin-bottom: 50px;
    width: 75%;
}

.newsli {
    padding-bottom: 6px;
    border-bottom: 1px solid #d1a9a9;
    margin-bottom: 9px;
    cursor: pointer;
}

.news_title {
    margin: 0 0 0 17px;
    width: 700px;
}

.news_time {
    font-size: 13px;
    color: #939393;
    line-height: 26px;
    position: absolute;
    right: 16px;
}

.newsTop {
    display: flex;
    position: relative;
    margin-bottom: 11px;
}

.badge {
    display: inline-block;
    color: white;
    padding: 5px 9px;
    width: 25px;
    border-radius: 8px;
    font-size: 11px;
    text-align: center;
    box-shadow: 0 2px 5px rgb(0 0 0 / 20%);
    height: 16px;
}

.news_content {
    margin-bottom: 9px;
    width: 755px;
    padding-right: 28px;
    font-size: 20px;
    padding-top: 4px;
}

.news_img {
    width: 245px;
    margin-right: 20px;
    height: 156px;
}

.relatedStockArea {
    display: flex;
    width: 866px;
}

.relatedStock {
    margin-left: 6px;
    font-size: 12px;
    color: #686868;
    margin-top: 5px;
}

.news_content_area {
    display: flex;
}

.newsType {
    margin-left: 27px;
}
</style>